import dayjs from "dayjs";
import Big from "big.js";
import { TableResultObject } from "./interface";
import weekday from "dayjs/plugin/weekday";
dayjs.extend(weekday); //  use

// 处理接口返回数据
export function resolveSearchData(data: any) {
  let result: any = [];
  if (data?.length) {
    data.map((item: any) => {
      let demandTotalQuantity = 0,
        feedbackTotalQuantity = 0,
        confirmTotalQuantity = 0;
      for (let i = 0; i < item.scheduleList.length; i++) {
        // 计算计划行总计数量
        if (item.scheduleList[i]?.demandStatus == 3) {
          demandTotalQuantity = new Big(demandTotalQuantity).plus(
            item.scheduleList[i].confirmQuantity || 0
          );
        } else {
          demandTotalQuantity = new Big(demandTotalQuantity).plus(
            item.scheduleList[i].demandQuantity || 0
          );
        }
        // 计算确认行总计数量
        confirmTotalQuantity = new Big(confirmTotalQuantity).plus(
          item.scheduleList[i].confirmQuantity || 0
        );
      }

      // 计算反馈行总计数量
      for (let i = 0; i < item.feedbackList.length; i++) {
        feedbackTotalQuantity = new Big(feedbackTotalQuantity).plus(
          item.feedbackList[i].feedbackTotal || 0
        );
      }

      const elderItem = {
        ...item,
        demandTotalQuantity,
        feedbackTotalQuantity,
        confirmTotalQuantity,
        // key: !item.id ? generateUniqueKey() : "",
      };
      const oneLine: any = [elderItem, elderItem, elderItem];
      result = [...result, ...oneLine];
    });
  }

  return result;
}

// 前端唯一值计算
export function generateUniqueKey() {
  const key = "key_";
  const time = Date.now().toString(36);
  const random = Math.random().toString(36).substr(2, 9);
  return key + time + random;
}

// 单元格合并 param表格数据  rowCount合并行数  columnCount合并前多少行
export const objectSpanMethod = (
  param: any,
  rowCount: number,
  columnCount: number
) => {
  if (param.columnIndex < columnCount) {
    if (param.rowIndex % rowCount === 0) {
      return {
        rowspan: rowCount,
        colspan: 1,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};

// 获取从startDate开始，向后推intervalDay的所有天数
export function getDays(startDate: any, intervalDay: any) {
  const newIntervalDay = intervalDay + 1;
  // 创建起始日期的Dayjs对象
  const dayjsStartDate = dayjs(startDate);

  // 获取下一个月的第一天
  const nextMonthFirstDay = dayjsStartDate
    .startOf("day")
    .add(newIntervalDay, "day");

  // 使用一个循环来获取所有日期
  let currentDate = dayjsStartDate;
  const dates = [];

  while (currentDate.isBefore(nextMonthFirstDay)) {
    const row = {};
    row.date = currentDate.format("MM/DD");
    row.week = currentDate.weekday();
    dates.push(row);
    // dates.push(currentDate.format("MM/DD"));
    currentDate = currentDate.add(1, "day");
  }
  return dates;
}

// 头部样式处理函数
export function leftHeaderStyle({ columnIndex }: any) {
  if (columnIndex === 0) {
    return "seltAllbtnDis";
  }
}

// el-table checkbox是否可选中处理函数
export function selectableStatus(row: TableResultObject) {
  // 如果行数据中有disabled属性，则返回false禁用多选
  if (row.id == "-999") {
    return false;
  } else {
    return !row.disabled;
  }
}

// 颜色处理方法
export function inConsistent(
  row: TableResultObject,
  $index: number,
  dayIndex: number
) {
  let className = "";
  // 计划数量
  if (($index + 1) % 3 == 1) {
    if (row.scheduleList[dayIndex]?.colorType == 0) {
      className = "";
    } else if (row.scheduleList[dayIndex]?.colorType == 1) {
      className = "#95D475";
    } else if (row.scheduleList[dayIndex]?.colorType == 2) {
      className = "#FEF1F1";
    } else if (row.scheduleList[dayIndex]?.colorType == 3) {
      className = "#FFFDBD";
    } else if (
      row.scheduleList[dayIndex]?.colorType == 4 &&
      row.scheduleList[dayIndex]?.demandStatus != 3
    ) {
      className = "#55EFC4";
    } else if (row.scheduleList[dayIndex]?.colorType == 5) {
      className = "#FFD700";
    }
  }
  // 反馈数量样式
  if (($index + 1) % 3 == 2) {
    // if (
    //   row.feedbackList[dayIndex]?.feedbackTotal &&
    //   row.feedbackList[dayIndex]?.feedbackTotal !=
    //     (row.scheduleList[dayIndex]?.confirmQuantity
    //       ? row.scheduleList[dayIndex]?.confirmQuantity
    //       : row.scheduleList[dayIndex]?.demandQuantity)
    // ) {
    //   className = "c-red-font";
    // }
    if (row.feedbackList[dayIndex]?.colorType == 0) {
      className = "";
    } else if (row.feedbackList[dayIndex]?.colorType == 1) {
      className = "#95D475";
    } else if (row.feedbackList[dayIndex]?.colorType == 2) {
      className = "#FEF1F1";
    } else if (row.feedbackList[dayIndex]?.colorType == 3) {
      className = "#FFFDBD";
    } else if (row.feedbackList[dayIndex]?.colorType == 4) {
      className = "#55EFC4";
    } else if (row.feedbackList[dayIndex]?.colorType == 5) {
      className = "#FFD700";
    }
  }
  // 确认数量样式
  if (($index + 1) % 3 == 0 && row.scheduleList[dayIndex]?.demandStatus == 3) {
    // 允许发货变成绿色
    // if (row.scheduleList[dayIndex]?.allowSend == 1) {
    //   className = "c-green";
    // }
    if (row.scheduleList[dayIndex]?.colorType == 0) {
      className = "";
    } else if (row.scheduleList[dayIndex]?.colorType == 1) {
      className = "#95D475";
    } else if (row.scheduleList[dayIndex]?.colorType == 2) {
      className = "#FEF1F1";
    } else if (row.scheduleList[dayIndex]?.colorType == 3) {
      className = "#FFFDBD";
    } else if (row.scheduleList[dayIndex]?.colorType == 4) {
      className = "#55EFC4";
    } else if (row.scheduleList[dayIndex]?.colorType == 5) {
      className = "#FFD700";
    }
  }
  return className;
}
